/**
 * Created by cchen on 2015/5/19.
 */


'use strict';
angular.module('backofficeApp.retailerBalance.controller', []).
    controller('retailerBalanceCtrl', ['$scope', '$http', function ($scope, $http) {

        $scope.startDate = datePicker("#startDate");
        $scope.endDate = datePicker("#endDate");
        $scope.settleDate = datePicker("#settleDate");

        function datePicker(id) {
            function d(id) {
                var self = this;
                $(id).datepicker({
                    format: 'yyyy-mm-dd',
                    autoclose: true,
                    forceParse: false
                }).on('changeDate', function (e) {
                    self.date = e.date;
                }).on('show', function (e) {
                    $(".datepicker.datepicker-dropdown.dropdown-menu").css("z-index", "10000");

                });

            }

            d.prototype.getValue = function (isFrom) {

                if ($(id).val() == "") {
                    return ''
                }

                if (this.date == null) {
                    return '';
                }
                if (isFrom) {
                    return this.date.setHours(0, 0, 0, 0)
                }
                return this.date.setHours(23, 59, 59, 0)
            }
            var date = new d(id);
            return date;
        }

        $scope.getRetailerSettlement = function (opt) {

            if (opt == null) {
                opt = {}
            }
            opt.startTime = $scope.startDate.getValue(true);
            opt.endTime = $scope.endDate.getValue(false);

            console.log(opt);

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/voucher/getRetailerSettlement', opt, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.settlementList = data.data.list;
                $scope.balanceOpt = data.data.opt;
            }).error(function () {
                $scope.settlementList = null;
            });
        };

        $scope.getRetailerSettlement();


        $scope.setId = function (id) {
            $scope.balanceOpt.id = id;
            $('#settleModal').modal('show')
        };

        $scope.saveBalance = function () {
            if ($scope.settleDate.getValue(true) == '') {
                return;
            }
            $scope.balanceOpt.closeTime = $scope.settleDate.getValue(true);
            console.log($scope.balanceOpt);

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/voucher/retailerBalance', $scope.balanceOpt, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.getRetailerSettlement($scope.opt);
                $('#settleModal').modal('hide')
            }).error(function () {
            });
        }
    }]);


angular.module('backofficeApp.retailerBalance.services', ['ngResource']).
    factory('retailerBalanceService', function ($resource) {
        return {
            balance_list: $resource('http://' + backOfficeHost + '/rest/v1.0/backoffice/voucher/balance/list', {}, {}),
            retailer_list: $resource('http://' + backOfficeHost + '/rest/v1.0/backoffice/voucher/getRetailerList/all', {}, {})
        }
    })